<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <!--<a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="店铺">
&lt;!&ndash;            <j-dict-select-tag placeholder="请选择店铺" v-model="queryParam.shopId" dictCode="mk_shop_basic,name,id"/>&ndash;&gt;
            <a-select type="list_multi" v-model="queryParam.shopId" placeholder="请选择店铺" >

              <a-select-option v-for="(i, k) in shopList" :value="i.id" :key="i.id">
                {{i.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>-->
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="店铺名称">
            <a-input placeholder="输入店铺名称查询" v-model="queryParam.shopName"></a-input>
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="商品名称">
            <a-input placeholder="输入商品名称查询" v-model="queryParam.name"></a-input>
          </a-form-item>
        </a-col>
        <!--  <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="删除状态">
              <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.delFlag" dictCode="mk_whether"/>
            </a-form-item>
          </a-col>-->
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <a-form-item label="上下架状态">
            <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.status" dictCode="mk_goods_status" />
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24">
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            <a @click="handleToggleSearch" style="margin-left: 8px">
              {{ toggleSearchStatus ? '收起' : '展开' }}
              <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
            </a>
          </span>
        </a-col>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('商品表')">导出</a-button>
       <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                 @change="handleImportExcel">
         <a-button type="primary" icon="import">导入</a-button>
       </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery">
      </j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap" @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <!--<span slot="delFlag" slot-scope="text, record">
           <a-popconfirm :title="`确定${record.delFlag==0?'删除':'恢复'}该商品分类吗?`" @confirm="() => onChangeDelFlag(record)">
           <a-switch checked-children="已删除" un-checked-children="未删除"
                     :checked="record.delFlag=== 1 ? true : false" />
           </a-popconfirm>
        </span>-->

        <span slot="status" slot-scope="text, record">
          <a-popconfirm :title="`确定${record.status==0?'上':'下'}架吗?`" @confirm="() => onChangeStatus(record)">
            <a-switch checked-children="上架" un-checked-children="下架" :checked="record.status=== '1' ? true : false" />
          </a-popconfirm>
        </span>

        <!-- <span slot="isIndex" slot-scope="text, record">
          <a-popconfirm :title="`确定将商品从首页${record.isIndex==0?'展示':'隐匿'}吗?`" @confirm="() => onChangeIsIndex(record)">
            <a-switch checked-children="是" un-checked-children="否" :checked="record.isIndex === '1' ? true : false" />
          </a-popconfirm>
        </span> -->
        <span slot="isHot" slot-scope="text, record">
          <a-popconfirm :title="`确定将商品设置为${record.isHot==0?'热门商品':'一般商品'}吗?`" @confirm="() => onChangeIsHot(record)">
            <a-switch checked-children="是" un-checked-children="否" :checked="record.isHot === '1' ? true : false" />
          </a-popconfirm>
        </span>
        <span slot="oneNoDelivery" slot-scope="text, record">
          <a-popconfirm :title="`确定配送为${record.oneNoDelivery==0?'单点不送':'无限制'}吗?`" @confirm="() => onChangeOneNoDelivery(record)">
            <a-switch checked-children="是" un-checked-children="否" :checked="record.oneNoDelivery === '1' ? true : false" />
          </a-popconfirm>
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <!--           <a @click="handleDetail(record)">详情</a>-->
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
    <GoodsBasicModal ref="modalForm" @ok="modalFormOk"></GoodsBasicModal>
  </a-card>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GoodsBasicModal from './modules/GoodsBasicModal.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'GoodsBasicList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    GoodsBasicModal
  },
  data() {
    return {
      message: "hello vue",
      shopList: [],
      description: '商品表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        // {
        //   title: '门店',
        //   align: 'center',
        //   dataIndex: 'shopId_dictText'
        // },
        {
          title: '商品全称',
          align: 'center',
          dataIndex: 'name'
        },
        // {
        //     title: '商品简称',
        //     align: 'center',
        //     dataIndex: 'shortName'
        // },
        {
          title: '商品品类',
          align: 'center',
          dataIndex: 'kategorieId_dictText'
        },
        // {
        //   title: '店内分类',
        //   align: 'center',
        //   dataIndex: 'sorts_dictText'
        // },
        {
          title: '商品原价',
          align: 'center',
          dataIndex: 'originalPrice'
        },
        {
          title: '会员价',
          align: 'center',
          dataIndex: 'presentPrice'
        },
        {
          title: '商品图片',
          align: 'center',
          dataIndex: 'picture',
          scopedSlots: { customRender: 'imgSlot' }
        },
        // {
        //     title: '商品简单描述',
        //     align: 'center',
        //     dataIndex: 'shortDescription'
        // },
        {
          title: '商品上下架状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        // {
        //   title: '是否首页展示',
        //   align: 'center',
        //   dataIndex: 'isIndex_dictText',
        //   scopedSlots: { customRender: 'isIndex' }
        // },
        // {
        //   title: '是否热门产品',
        //   align: 'center',
        //   dataIndex: 'isHot_dictText',
        //   scopedSlots: { customRender: 'isHot' }
        // },
        // {
        //   title: '是否拼团商品',
        //   align: 'center',
        //   dataIndex: 'enableGroup',
        //   filterMultiple: false,
        //   onFilter: (value, record) => record.status === value,
        //   customRender: (text) => {
        //     return text === '1' ? '否' : '是';
        //   }
        // },
        /*{
          title: '浏览次数',
          align: 'center',
          sorter: true,
          dataIndex: 'visitNum'
        },
        {
          title: '销售数量',
          align: 'center',
          sorter: true,
          dataIndex: 'saleNum'
        },*/
        /*{
            title: '是否删除',
            align: 'center',
            dataIndex: 'delFlag_dictText',
            scopedSlots: { customRender: 'delFlag' }
        },*/
        {
          title: '排序(从大到小)',
          align: 'center',
          dataIndex: 'sort'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      queryParam: {
        goodBrand: localStorage.getItem('myDataId')
      },
      url: {
        list: '/goods/goodsBasic/list',
        delete: '/goods/goodsBasic/delete',
        deleteBatch: '/goods/goodsBasic/deleteBatch',
        exportXlsUrl: '/goods/goodsBasic/exportXls',
        importExcelUrl: 'goods/goodsBasic/importExcel',
        statusChangeUrl: '/goods/goodsBasic/statusChange',
        isIndexChangeUrl: '/goods/goodsBasic/isIndexChange',
        isHotChangeUrl: '/goods/goodsBasic/isHotChange',
        delFlagChangeUrl: '/goods/goodsBasic/delFlagChange',
        getShopListUrl: '/shop/shopBasic/selectShopList'

      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getShopListUrl()
    this.getSuperFieldList()
    this.localStorage()
    // // 从本地缓存获取数据
    // const storedData = localStorage.getItem('myDataId');
    // console.log(storedData);
    // if (storedData) {
    //   this.queryParam.goodBrand = storedData
    // }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    localStorage() {
      localStorage.setItem("library", 2);
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'shopId', text: '所属门店', dictCode: 'mk_shop_basic,name,id' })
      fieldList.push({ type: 'string', value: 'name', text: '商品全称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'shortName', text: '商品简称', dictCode: '' })
      fieldList.push({
        type: 'list_multi',
        value: 'sorts',
        text: '商品分类',
        dictTable: 'mk_goods_sort',
        dictText: 'name',
        dictCode: 'id'
      })
      fieldList.push({ type: 'BigDecimal', value: 'originalPrice', text: '商品原价', dictCode: '' })
      fieldList.push({ type: 'string', value: 'presentPrice', text: '商品现价', dictCode: '' })
      fieldList.push({ type: 'string', value: 'code', text: '商品代码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'picture', text: '商品图片', dictCode: '' })
      fieldList.push({ type: 'string', value: 'description', text: '商品完整描述', dictCode: '' })
      fieldList.push({ type: 'string', value: 'shortDescription', text: '商品简单描述', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '商品上下架状态', dictCode: 'mk_goods_status' })
      fieldList.push({ type: 'string', value: 'isIndex', text: '是否首页展示', dictCode: 'mk_whether' })
      fieldList.push({ type: 'string', value: 'isHot', text: '是否热门产品', dictCode: 'mk_whether' })
      fieldList.push({ type: 'int', value: 'visitNum', text: '浏览次数', dictCode: '' })
      fieldList.push({ type: 'int', value: 'saleNum', text: '销售数量', dictCode: '' })
      fieldList.push({ type: 'string', value: 'delFlag', text: '是否删除', dictCode: 'mk_whether' })
      fieldList.push({ type: 'int', value: 'sort', text: '排序', dictCode: '' })
      this.superFieldList = fieldList
    },
    start() {
      this.loading = true
      // ajax request after empty completing
      setTimeout(() => {
        this.loading = false
        this.selectedRowKeys = []
      }, 1000)
    },
    getShopListUrl() {
      this.shopList = []
      getAction(this.url.getShopListUrl).then(res => {
        if (res.success) {
          let result = res.result
          result.forEach((e) => {
            this.shopList.push(e)
          })
        }
      })
      console.log(this.shopList)

    },
    onChangeStatus(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.statusChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })
    },
    onChangeIsIndex(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.isIndexChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()

        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })

    },
    onChangeIsHot(record) {
      console.log(record)
      var that = this
      that.loading = true
      getAction(this.url.isHotChangeUrl + '/' + record.id).then(res => {
        if (res.success) {
          //重新计算分页问题
          record = res.result
          that.reCalculatePage(that.selectedRowKeys.length)
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()

        } else {
          that.$message.warning('操作异常,请稍后再试')
        }
      }).finally(() => {
        that.loading = false
      })

    }
    /*onChangeDelFlag(record) {
        var that = this;
        that.loading = true;
        getAction(this.url.delFlagChangeUrl+"/"+record.id).then(res => {
            if (res.success) {
                record.delFlag = res.result.delFlag;
                //重新计算分页问题
                that.reCalculatePage(that.selectedRowKeys.length)
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
            } else {
                that.$message.warning("操作异常,请稍后再试");
            }
        }).finally(() => {
            that.loading = false;
        });
    },*/
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>